package model;

import inf.minife.fe.Constraint;
import inf.minife.fe.DOF;
import inf.minife.fe.Force;
import inf.minife.fe.Model;
import inf.minife.fe.Node;
import inf.minife.fe.RectangleS;
import inf.minife.fe.Truss2D;
//import inf.minife.view2.Viewer2;


public class TwoBar {

	private Model model;

	public static void main(String[] args) {
		Model model = new TwoBar().getModel();

		model.printStructure();
		model.solve();
		model.printResults();

		//Viewer2 viewer = new Viewer2(model);
		//viewer.setVisible(true);
	}

	public TwoBar() {

		// model
		model = new Model();

		double E = 210000; // N/mm^2 (modulus of elasticity)
		double rho = 7.88E-6; // kg/mm^2 (density of steel)
		model.createMaterial(1, E, rho);

		for (int i = 1; i <= 3; i++) {
			RectangleS r;
			r = (RectangleS) model.createSection(i, RectangleS.TYPE, Truss2D.TYPE);
			r.setTKY(10); // mm
			r.setTKZ(10); // mm

		}

		// nodes
		double B = 1000.0; // mm
		double H = 1000.0; // mm
		model.createNode(1, -B, 0, 0);
		model.createNode(2, B, 0, 0);
		model.createNode(3, 0, -H, 0);

		// forces
		Force f = new Force();
		f.setValue(DOF.T_X, 30000.0); // N
		f.setValue(DOF.T_Y, -20000.0); // N
		model.getNode(3).setForce(f);

		// constraints
		Constraint c = new Constraint();
		c.setFree(DOF.T_X, false);
		c.setFree(DOF.T_Y, false);
		model.getNode(1).setConstraint(c);
		model.getNode(2).setConstraint(c);

		// elements
		model.createElement(1, Truss2D.TYPE, model.getMaterial(1), model
				.getRealtable(1), model.getNode(1), model.getNode(3));
		model.createElement(2, Truss2D.TYPE, model.getMaterial(1), model
				.getRealtable(2), model.getNode(2), model.getNode(3));
	}
	
	public Model createModel(Model newmodel, double[] x){
		/* Pass new value to model and print out */
		double B = x[0];

		newmodel.getNode(1).setCoordinate(Node.X, -B);
		newmodel.getNode(2).setCoordinate(Node.X, B);

		RectangleS r;

		for (int i = 1; i < x.length; i++) {
			r = (RectangleS) newmodel.getRealtable(i);
			r.setTKY(x[i]);
			r.setTKZ(x[i]);
		}

		// run analysis
		newmodel.solve();
		return newmodel;
	}
	
	public Model getModel() {
		return model;
	}
}